// subpkg_worker/login/login.js
import Toast from '@vant/weapp/toast/toast';
import md5 from '../../utils/md5.js';
import Dialog from '@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    show1:false,
    name:'',
    phone:'',
    identity:'',
    careHomeName:'',
    careHomeColumns:[],
    identityColumns:['家属','老人','护理员']

  },
  bindName(e){
    this.setData({
      name:e.detail.value
    })
  },
  bindPhone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  bindHeadhouse(){
    this.setData({
      show:true
    })
  },
  bindIdentity(){
    this.setData({
      show1:true
    })
  },
  onChange(event) {
    var index = event.detail.index
    this.setData({
      careHomeName:this.data.careHomeColumns[index]
    })
  },
  onChange1(event) {
    var index = event.detail.index
    this.setData({
      identity:this.data.identityColumns[index]
    })
  },
  confirm(e){
    console.log(e.detail)
    if(e.detail.index===0){
      this.setData({
        careHomeName:this.data.careHomeColumns[0],
        show:false
      })
      return
    }
    this.setData({
      careHomeName:this.data.careHomeName,
      show:false
    })
  },
  confirm1(e){
    if(e.detail.index===0){
      this.setData({
        identity:this.data.identityColumns[0],
        show1:false
      })
      return
    }
    this.setData({
      identity:this.data.identity,
      show1:false
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  onClose1(){
    this.setData({
      show1:false
    })
  },
  cancel(){
    this.setData({
      show:false
    })
  },
  cancel1(){
    this.setData({
      show1:false
    })
  },
  login(){
    if(this.data.careHomeName===''||this.data.identity===''){
      Toast('请完善信息')
      return
    }
    var identity = this.data.identity
    if(identity==='家属'){
      identity = -1
    }
    else if(identity==='老人')
    {
      identity = 0
      wx.request({
        url: `https://zhixiao.site:8080/client/toElderRole?name=${wx.getStorageSync('name')}&careHomeName=${this.data.careHomeName}&openid=${wx.getStorageSync('openid')}&iconUrl=${wx.getStorageSync('avatarUrl')}`,
        success:(res)=>{
          console.log(res)
          if(res.data.code==="200"){
            wx.setStorageSync('identity', '老人')
            wx.setStorageSync('beadhouse', this.data.careHomeName)
            wx.redirectTo({
              url: '/elder/service/service',
            })
          }else{
            Toast('再按一次"确定"完成登录')
          }
          
        }
      })
      return
    }else{
      identity = 1
    }
    wx.request({
      url: `https://zhixiao.site:8080/identity/send-request?userId=${wx.getStorageSync('userid')}&role=${identity}&careHomeName=${this.data.careHomeName}`,
      method:'POST',
      header: {  
        "Content-Type": "application/json"
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code==="200"){
          Dialog.alert({
            message: '身份信息申请中',
          }).then(() => {
            // on close
            wx.switchTab({
              url: '/pages/home/home',
            })
          });
        }
        // var identity = res.data.data.identity
        // if(identity === '老人'){
        //   wx.navigateTo({
        //     url: '/elder/service/service',
        //   })
        // }else if(identity === '家属'){
        //   wx.switchTab({
        //     url: '/pages/home/home',
        //   })
        // }else{
        //   wx.switchTab({
        //     url: '/pages/elderList/elderList',
        //   })
        // }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.request({
      url: 'https://zhixiao.site:8080/client/home/name/query',
      success:(res)=>{
        var careHomeColumns = []
        var careHomeNames = res.data.data
        console.log(careHomeNames)
        for(var i in careHomeNames)
        {
          careHomeColumns.push(careHomeNames[i].name)
        }
        this.setData({
          careHomeColumns:careHomeColumns
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})